import random

from otree.api import (
    models,
    widgets,
    BaseConstants,
    BaseSubsession,
    BaseGroup,
    BasePlayer,
    Currency as c,
    currency_range,
)


author = 'Your name here'

doc = """
Your app description
"""


class Constants(BaseConstants):
    name_in_url = 'group3'
    players_per_group = 3
    num_rounds = 1


class Subsession(BaseSubsession):
    def creating_session(self):  # 分组
        #已知队伍分组 r1-r6和b1到b6，g1-g6 接下来要随机分队

        matrix1=[self.session.vars['r1'], self.session.vars['r2'], self.session.vars['r3'], self.session.vars['r4'], self.session.vars['r5'], self.session.vars['r6']]
        matrix2=[self.session.vars['b1'], self.session.vars['b2'], self.session.vars['b3'], self.session.vars['b4'], self.session.vars['b5'], self.session.vars['b6']]
        matrix3 = [self.session.vars['g1'], self.session.vars['g2'], self.session.vars['g3'], self.session.vars['g4'],
                   self.session.vars['g5'], self.session.vars['g6']]
        print(matrix1)
        print(matrix2)
        print(matrix3)
        # group3 3个人 2个人是一个群体 一个人是另外群体的
        # 112 113 221 223 331 332
        selection = random.choice(range(len(matrix1)))
        vs_r1_a = matrix1.pop(selection)
        selection = random.choice(range(len(matrix1)))
        vs_r1_b = matrix1.pop(selection)
        selection = random.choice(range(len(matrix2)))
        vs_r1_c = matrix2.pop(selection)

        selection = random.choice(range(len(matrix1)))
        vs_r2_a = matrix1.pop(selection)
        selection = random.choice(range(len(matrix1)))
        vs_r2_b = matrix1.pop(selection)
        selection = random.choice(range(len(matrix3)))
        vs_r2_c = matrix3.pop(selection)

        selection = random.choice(range(len(matrix2)))
        vs_r3_a = matrix2.pop(selection)
        selection = random.choice(range(len(matrix2)))
        vs_r3_b = matrix2.pop(selection)
        selection = random.choice(range(len(matrix1)))
        vs_r3_c = matrix1.pop(selection)

        selection = random.choice(range(len(matrix2)))
        vs_r4_a = matrix2.pop(selection)
        selection = random.choice(range(len(matrix2)))
        vs_r4_b = matrix2.pop(selection)
        selection = random.choice(range(len(matrix3)))
        vs_r4_c = matrix3.pop(selection)

        selection = random.choice(range(len(matrix3)))
        vs_r5_a = matrix3.pop(selection)
        selection = random.choice(range(len(matrix3)))
        vs_r5_b = matrix3.pop(selection)
        selection = random.choice(range(len(matrix1)))
        vs_r5_c = matrix1.pop(selection)

        selection = random.choice(range(len(matrix3)))
        vs_r6_a = matrix3.pop(selection)
        selection = random.choice(range(len(matrix3)))
        vs_r6_b = matrix3.pop(selection)
        selection = random.choice(range(len(matrix2)))
        vs_r6_c = matrix2.pop(selection)



        #分组后
        matrix =[[vs_r1_a,vs_r1_b,vs_r1_c],[vs_r2_a,vs_r2_b,vs_r2_c],[vs_r3_a,vs_r3_b,vs_r3_c],
                 [vs_r4_a,vs_r4_b,vs_r4_c],[vs_r5_a,vs_r5_b,vs_r5_c],[vs_r6_a,vs_r6_b,vs_r6_c]]

        #第一次对抗 但是其实没有second_contest使用的机会
        self.session.vars['first_contest']=matrix
        self.set_group_matrix(matrix)
        print("group3分组后：")
        # print(self.session.vars['first_contest'])
        print(self.get_group_matrix())


        #还需要把队友的Id展现给对方
        #已知r1-b6对应的队友Id
        #暂且不写




        # matrix = [[self.session.vars['r1'], self.session.vars['r2'], self.session.vars['r3'], self.session.vars['r4'], self.session.vars['r5'], self.session.vars['r6']],
        #           [self.session.vars['b1'], self.session.vars['b2'], self.session.vars['b3'], self.session.vars['b4'], self.session.vars['b5'], self.session.vars['b6']]]
        # self.set_group_matrix(matrix)


class Group(BaseGroup):
    pass


class Player(BasePlayer):
    pass
